﻿//-----------------------------------------------------------------------------
// <copyright company="Cortego AB, 2010" file="Program.cs">
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
// </copyright>
//-----------------------------------------------------------------------------

namespace Cortego.ADFS.FIMAttributeStore.EventSource
{
    using System;
    using System.Diagnostics;

    /// <summary>
    /// Simple commandline util for adding and remiving of event source to the windows event log.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Program Main method.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 3)
                {
                    // Add event source.
                    if (args[0].ToLower() == "/a" && !string.IsNullOrEmpty(args[1]) && !string.IsNullOrEmpty(args[2]))
                    {
                        AddEventSource(args[1], args[2]);
                    }
                    else
                    {
                        WriteUsage();
                    }
                }
                else if (args.Length == 2)
                {
                    // Delete event source.
                    if (args[0].ToLower() == "/d" && !string.IsNullOrEmpty(args[1]))
                    {
                        DeleteEventSource(args[1]);
                    }
                    else if (args[0].ToLower() == "/dl" && !string.IsNullOrEmpty(args[1]))
                    {
                        DeleteLog(args[1]);
                    }
                    else
                    {
                        WriteUsage();
                    }
                }
                else
                {
                    // Write usage information
                    WriteUsage();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Can't create or delete event source,try to run EVENTSOURCE with elevated privileges.");
                Console.WriteLine("-----------------------------------------------------");
                Console.WriteLine(e.Message);
                Console.WriteLine("-----------------------------------------------------");
            }
        }

        /// <summary>
        /// Adds an event source and eventually a log to windows event log.
        /// </summary>
        /// <param name="eventSourceName">The name of the event source to add.</param>
        /// <param name="logName">The name of the log the event source should be added to, if it doesn't exist it will be created.</param>
        private static void AddEventSource(string eventSourceName, string logName)
        {
            if (!EventLog.SourceExists(eventSourceName))
            {
                EventLog.CreateEventSource(eventSourceName, logName);
                EventLog.WriteEntry(eventSourceName, string.Format("{0} event source successfully created.", eventSourceName), EventLogEntryType.Information);
                Console.WriteLine("Successfully created {0} event source!", eventSourceName);
            }
            else
            {
                Console.WriteLine(string.Format("{0} event source already exists.", eventSourceName));
            }
        }

        /// <summary>
        /// Deletes an existing event source.
        /// </summary>
        /// <param name="eventSourceName">The name of the event source to delete.</param>
        private static void DeleteEventSource(string eventSourceName)
        {
            if (EventLog.SourceExists(eventSourceName))
            {
                EventLog.DeleteEventSource(eventSourceName);
                Console.WriteLine("Successfully deleted {0} event source!", eventSourceName);
            }
            else
            {
                Console.WriteLine(string.Format("{0} event source doesn't exist.", eventSourceName));
            }
        }

        /// <summary>
        /// Deletes an existing log.
        /// </summary>
        /// <param name="logName">The name of the log to delete.</param>
        private static void DeleteLog(string logName)
        {
            if (EventLog.Exists(logName))
            {
                EventLog.Delete(logName);
                Console.WriteLine("Successfully deleted the {0} log!", logName);
            }
            else
            {
                Console.WriteLine(string.Format("{0} log doesn't exist.", logName));
            }
        }

        /// <summary>
        /// Writes usage information to the command line.
        /// </summary>
        private static void WriteUsage()
        {
            Console.Write("Adds or deletes event source.\n\n");
            Console.Write("EVENTSOURCE [/A] [/D] [/DL] eventsource log\n\n");
            Console.Write("/A       Adds an event source\n");
            Console.Write("/D       Deletes an event source\n");
            Console.Write("/DL      Deletes a log\n\n");
            Console.Write("EVENTSOURCE command must be executed with elevated privileges\n\n");
            Console.Write("Usage:\nEVENTSOURCE /A eventsource log\nEVENTSOURCE /D eventsource\nEVENTSOURCE /DL log\n\n");
        }
    }
}
